package service.velocity;

import dao.inte.ConfigurationRepository;
import domain.MyConfiguration;
import domain.User;
import domain.shop.Order;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import web.admin.config.DirectoryPathException;

import java.io.StringWriter;
import java.util.Properties;

/**
 * This class provide dynamic creating of e-mail
 */
@Service
public class VelocityService {
    @Autowired(required = true)
    ConfigurationRepository configurationRepository;

    //injected by spring
    String orderMailTemplatePath;
    String registrationMailTemplatePath;
    String passwordResetRequestTemplatePath;
    String passwordResetTemplatePath;
    String passwordResetLink;
    String accountActivationMailTemplatePath;

    public String getAccountActivationMailTemplatePath() {
        return accountActivationMailTemplatePath;
    }

    public void setAccountActivationMailTemplatePath(String accountActivationMailTemplatePath) {
        this.accountActivationMailTemplatePath = accountActivationMailTemplatePath;
    }


    public VelocityService() {

    }

    private VelocityEngine getVelocityEngine() throws DirectoryPathException, Exception {
        String templateDirectory;
        MyConfiguration configuration = configurationRepository.getConfig();
        templateDirectory = configuration.getTemplateDirectory();
        Properties props = new Properties();
        props.put("resource.loader", "my");
        props.put("my.resource.loader.class", CustomVelocityLoader.class.getName());
        props.put("my.resource.loader.path", "");  // !!!
        props.put("my.resource.loader.cache", "true");
        props.put("input.encoding", configuration.getInputEncoding());
        props.put("output.encoding", configuration.getOutputEncoding());


        VelocityEngine ve = new VelocityEngine(props);
        ve.init();

        return ve;
    }

    public String getPasswordResetMail(User user, String password) throws DirectoryPathException, Exception {
        VelocityEngine ve = getVelocityEngine();

        VelocityContext context = new VelocityContext();
        context.put("user", user);
        context.put("password", password);
        /*
        *   get the Template
        */

        Template t = ve.getTemplate(passwordResetTemplatePath);
        /*
        *  now render the template into a Writer, here
        *  a StringWriter
        */

        StringWriter writer = new StringWriter();
        t.merge(context, writer);

        /*
        *  use the output in the body of your emails
        */
        return writer.toString();
    }

    public String getPasswordResetRequestMail(User user, String key) throws DirectoryPathException, Exception {
        VelocityEngine ve = getVelocityEngine();


        VelocityContext context = new VelocityContext();
        context.put("user", user);
        context.put("passwordResetLink", passwordResetLink);
        context.put("key", key);

        /*
         *   get the Template
         */

        Template t = ve.getTemplate(passwordResetRequestTemplatePath);
        /*
         *  now render the template into a Writer, here
         *  a StringWriter
         */

        StringWriter writer = new StringWriter();
        t.merge(context, writer);

        /*
         *  use the output in the body of your emails
         */
        return writer.toString();
    }

    public String getAccountActivationMail(User user) throws DirectoryPathException, Exception {
        VelocityEngine ve = getVelocityEngine();


        VelocityContext context = new VelocityContext();
        context.put("user", user);

        /*
         *   get the Template
         */

        Template t = ve.getTemplate(accountActivationMailTemplatePath);
        /*
         *  now render the template into a Writer, here
         *  a StringWriter
         */

        StringWriter writer = new StringWriter();
        t.merge(context, writer);

        /*
         *  use the output in the body of your emails
         */
        return writer.toString();
    }

    public String getRegistrationMail(User user) throws DirectoryPathException, Exception {
        VelocityEngine ve = getVelocityEngine();


        VelocityContext context = new VelocityContext();
        context.put("user", user);

        /*
         *   get the Template
         */

        Template t = ve.getTemplate(registrationMailTemplatePath);
        /*
         *  now render the template into a Writer, here
         *  a StringWriter
         */

        StringWriter writer = new StringWriter();
        t.merge(context, writer);

        /*
         *  use the output in the body of your emails
         */
        return writer.toString();
    }

    public String getOrderMail(Order order) throws Exception {
        VelocityEngine ve = getVelocityEngine();


        /*
         *   organize our data
         */

//        List<Person> persons = new ArrayList<Person>();
//        persons.add(new Person("Joe", "Down"));
//        persons.add(new Person("Fritz", "Frizel"));
//        persons.add(new Person("Flip", "Vlieger"));
//        persons.add(new Person("George", "Forrest"));


        /*
         *  add that list to a VelocityContext
         */

        VelocityContext context = new VelocityContext();
        context.put("order", order);
        context.put("items", order.getProducts());
        context.put("user", order.getUser());
        context.put("price", order.getTotalPrice());
        context.put("address", order.getDeliveryAddress());

        /*
         *   get the Template
         */
        Template t = ve.getTemplate(orderMailTemplatePath);

        /*
        *  now render the template into a Writer, here
        *  a StringWriter
        */

        StringWriter writer = new StringWriter();
        t.merge(context, writer);

        /*
         *  use the output in the body of your emails
         */


        return writer.toString();


    }


    public void setOrderMailTemplatePath(String orderMailTemplatePath) {
        this.orderMailTemplatePath = orderMailTemplatePath;
    }


    public void setPasswordResetRequestTemplatePath(String passwordResetRequestTemplatePath) {
        this.passwordResetRequestTemplatePath = passwordResetRequestTemplatePath;
    }

    public void setPasswordResetTemplatePath(String passwordResetTemplatePath) {
        this.passwordResetTemplatePath = passwordResetTemplatePath;
    }

    public void setPasswordResetLink(String passwordResetLink) {
        this.passwordResetLink = passwordResetLink;
    }

    public String getOrderMailTemplatePath() {
        return orderMailTemplatePath;
    }

    public String getPasswordResetRequestTemplatePath() {
        return passwordResetRequestTemplatePath;
    }

    public String getPasswordResetTemplatePath() {
        return passwordResetTemplatePath;
    }

    public String getRegistrationMailTemplatePath() {
        return registrationMailTemplatePath;
    }

    public void setRegistrationMailTemplatePath(String registrationMailTemplatePath) {
        this.registrationMailTemplatePath = registrationMailTemplatePath;
    }

    public String getPasswordResetLink() {
        return passwordResetLink;
    }

    public void setConfigurationRepository(ConfigurationRepository configurationRepository) {
        this.configurationRepository = configurationRepository;
    }

}
